package com.efast.dmom.mes.datahandlers.entity;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.efast.cafe.framework.util.SpringWebUtils;
import com.efast.dmom.mes.bean.Param;
import com.efast.dmom.mes.bean.ResultBean;
import com.efast.dmom.mes.dao.entity.ParamDao;
import com.efast.dmom.mes.datahandlers.base.ConfigurationDataHandler;
import com.efast.dmom.mes.entity.ParamEntity;
import com.efast.dmom.mes.util.TimeUtil;

@Component
public class ParamHandler extends ConfigurationDataHandler<Param>{
	@Autowired
	private ParamDao paramDao;
	
	@Override
	public ResultBean save(Param obj) {
		ResultBean resultBean = new ResultBean();
		if (obj.getInnerData() != null) {
			obj.getInnerData().setSiteId(DataHandlerUtils.getSiteId());
			obj.getInnerData().setCreationTime(TimeUtil.getLocalDateTime());
			obj.getInnerData().setCreationTimeZone(TimeUtil.getLocalTimeZone());
			obj.getInnerData().setCreator(SpringWebUtils.getRequestUser());
			obj.getInnerData().setLastModifiedTime(TimeUtil.getLocalDateTime());
			obj.getInnerData().setLastModifiedTimeZone(TimeUtil.getLocalTimeZone());
			obj.getInnerData().setLastModifier(SpringWebUtils.getRequestUser());
//			obj.getInnerData().setCreationPid(DataHandlerUtils.getDmomPid());
//			obj.getInnerData().setLastModifiedPid(DataHandlerUtils.getDmomPid());
			obj.getInnerData().setRowStatus(1);
			obj.getInnerData().setRowTrxId(1);
			paramDao.save(obj.getInnerData());
		}
		return resultBean;
	}
	
	@Override
	public ResultBean update(Param obj) {
		ResultBean resultBean = new ResultBean();
		if (obj.getInnerData() != null) {
			obj.getInnerData().setLastModifiedTime(TimeUtil.getLocalDateTime());
			obj.getInnerData().setLastModifier(SpringWebUtils.getRequestUser());
			obj.getInnerData().setLastModifiedTimeZone(TimeUtil.getLocalTimeZone());
//			obj.getInnerData().setLastModifiedPid(DataHandlerUtils.getDmomPid());		
			paramDao.update(obj.getInnerData());
		}
		return resultBean;
	}
	
	@Override
	public ResultBean delete(Param obj) {
		ResultBean resultBean = new ResultBean();
		if (obj.getInnerData() != null) {
			obj.getInnerData().setLastModifiedTime(TimeUtil.getLocalDateTime());
			obj.getInnerData().setLastModifier(SpringWebUtils.getRequestUser());
			obj.getInnerData().setLastModifiedTimeZone(TimeUtil.getLocalTimeZone());
//			obj.getInnerData().setLastModifiedPid(DataHandlerUtils.getDmomPid());		
			paramDao.delete(obj.getInnerData());
		}
		return resultBean;
	}
	
	public List<Param> getByParamGroupId (long paramGroupId){
		
		List<Param> ParamList = new ArrayList<Param>();
		List<ParamEntity> ParamEntityList = paramDao.getByParamGroupId(paramGroupId);
		if(ParamEntityList!=null && ParamEntityList.size()>0) {
			for(ParamEntity list:ParamEntityList) {
				Param param = new Param();
				param.setInnerData(list);
				ParamList.add(param);
			}
			return ParamList;
		}
		return null;
	}
	public void deleteByParamGroupId(Param obj){
		if(obj.getInnerData() !=null) {
			obj.getInnerData().setLastModifiedTime(TimeUtil.getLocalDateTime());
			obj.getInnerData().setLastModifier(SpringWebUtils.getRequestUser());
			obj.getInnerData().setLastModifiedTimeZone(TimeUtil.getLocalTimeZone());
//			obj.getInnerData().setLastModifiedPid(DataHandlerUtils.getDmomPid());
			paramDao.deleteByParamGroupId(obj.getInnerData());;
		}
	}
	public Param getByNameAndParamGroupId(long paramGroupId,String name) {
		Param param = new Param();
		ParamEntity paramEntity = paramDao.getByNameAndParamGroupId(paramGroupId,name);
		if(paramEntity !=null) {
			param.setInnerData(paramEntity);
		}
		return param;
		
	}
	
	public Param getById(long paramId) {
		Param param = new Param();
		ParamEntity paramEntity = paramDao.getById(paramId);
		if(paramEntity !=null) {
			param.setInnerData(paramEntity);
		}
		return param;
		
	}

	@Override
	public ResultBean saveBatch(List<Param> obj) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ResultBean updateBatch(List<Param> obj) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ResultBean deleteBatch(List<Param> obj) {
		// TODO Auto-generated method stub
		return null;
	}
}
